/*
  学习目标：setState第二种写法
  场景： setState产生了覆盖效果， 💥立马使用第二种写法
  语法： setState((旧的state) => {
    return 新的state
  })
*/

import React, { Component } from 'react';

export default class App extends Component {
  state = {
    count: 0,
  };

  handleAdd = () => {
    // setState永远都是异步的
    this.setState((preState) => {
      console.log('preState.count  ----->  ', preState.count); // 0
      return {
        count: preState.count + 1,
      };
    });
    this.setState((preState) => {
      console.log('preState.count  ----->  ', preState.count); // 1
      return {
        count: preState.count + 2,
      };
    });
    this.setState((preState) => {
      console.log('preState.count  ----->  ', preState.count); // 3
      return {
        count: preState.count + 3,
      };
    });
  };

  render() {
    return (
      <div>
        <h1>{this.state.count}</h1>
        <button onClick={this.handleAdd}>+1</button>
      </div>
    );
  }
}
